import React from 'react';
import Grid from '../../components/table/grid';
import { injectIntl } from 'react-intl';
import moment from 'moment';
import {Form ,Row ,Col, Select ,Button,Input,Divider,Modal} from 'antd';
import axios from 'axios';
import qs from 'qs';
import { sessionManager } from '../../utils/sessionStorage';
const FormItem = Form.Item;
const createForm = Form.create;
const Option = Select.Option;
const confirm = Modal.confirm;
class merchantLedgerOrder extends React.Component {
	constructor(props) {
		super(props);
		this.columns = [
			{
				title: '订单号',
				width: 150,
				key: 'orderId',
				dataIndex:'orderId',
				fixed: 'left',
			},
			{
				title: '负责人',
				width: 150,
				key: 'chiefName',
				dataIndex:'chiefName',
			},
			{
				title: '分账账号',
				width: 150,
				key: 'account',
				dataIndex:'account',
			},
			{
				title: '桩群名称',
				width: 200,
				key: 'groupName',
				dataIndex:'groupName',
			},
			{
				title: '订单时间',
				dataIndex: 'splitTime',
				key: 'splitTime',
				width: 150,
			},
			{
				title: '电量（KWH）',
				dataIndex: 'electric',
				key: 'electric',
				width: 150,
			},
			{
				title: '电费（元）',
				dataIndex: 'electricAmount',
				key: 'electricAmount',
				width: 150,
			},
			{
				title: '服务费（元）',
				width: 150,
				dataIndex: 'serviceAmount',
				key: 'serviceAmount',
			},
			{
				title: '总费用（元）',
				dataIndex: 'totalAmount',
				key: 'totalAmount',
				width: 150,
			},
			{
				title: '分账来源',
				key: 'settleType',
				width: 150,
				render: (text) => {
					switch (text.settleType) {
						case 0:
							return '普通订单';
						case 1:
							return '折扣分账';
						case 2:
							return 'SAAS分账';
						default:
							return ''
					}

				}
			}
			// {
			// 	title: '电费折扣',
			// 	dataIndex: 'settlementMoney',
			// 	key: 'settlementMoney',
			// 	width: 150,
			// },
			// {
			// 	title: '服务费折扣',
			// 	key: 'status',
			// 	dataIndex: 'settlementMoney',
			// 	width: 150,
			// }
		];
		this.state = {
			refresh:0,
			params:{

			}
		};
	}

	componentWillMount(){
		let newparams = {
			settlementUserId:sessionManager.get('rowRecord').settlementUserId,
			settleUserId:sessionManager.get('rowRecord').settleUserId,
			settlementDay:'',
			stubGroupId:sessionManager.get('rowRecord')['STUB_GROUP_ID'],
		}
		if(this.props.location.state&&this.props.location.state.orderRecord){
				newparams.settlementDay=this.props.location.state.orderRecord.settlementDay
		}
		this.setState(Object.assign(this.state.params,newparams),()=>{console.log('11111111111111-----',this.state.params)});
	}

	statusChange(val,option){
		console.log('11111',val,option)
	};
	inquireBillList=(e)=>{
		e.preventDefault();
		this.props.form.validateFieldsAndScroll((errors, values) => {
			if (!!errors) {
				return;
			}
			// console.info('Received values of form:',this.props.location.state.rowRecord,values);

			this.setState(Object.assign(this.state.params,values));
			this.setState({
				refresh:this.state.refresh+1
			})
		});
	};
	exportAllLists(){
		console.log('导出列表')
	};
	freshGrid = () => {
		this.setState({
			refresh: this.state.refresh + 1
		});
	};
	showConfirm=(text)=>{
		confirm({
			title: '确认完成结算?',
			content: '',
			onOk:()=> {
				axios.get('/api/settlement/billSplit/completeSettlement',{ params:{id: text.id}}).then(res => {
					if(res&&res.code==200){
						this.freshGrid()
					}
				});
			},
			onCancel() {
				console.log('Cancel');
			},
		});
	}
	goLink = (text, record, link) => {
		sessionManager.clearItem('rowRecord');
		this.props.history.push({pathname: `/finance/${link}`,state: {rowRecord: text}});
	};
	render() {
		const {getFieldDecorator} = this.props.form;
		const token=JSON.parse(sessionStorage.getItem("token")||{}).token;
		let exportQs=`${qs.stringify(this.state.params)}&token=${token}`;
		return (
			<div>
				<Form layout="inline" onSubmit={this.inquireBillList} className="">
					{/*<FormItem label="订单编号">
						{getFieldDecorator('userName', {
						})(<Input placeholder="模糊匹配：订单编号" />)}
					</FormItem>
					<FormItem label="桩群名称">
						{getFieldDecorator('account', {
						})(<Input placeholder="模糊匹配：桩群名称" />)}
					</FormItem>*/}
					<FormItem label="分账来源">
						{getFieldDecorator('settleType', {
							initialValue : ''
						})(<Select
							allowClear={true}
							style={{ width: 160 }}
							onChange={this.statusChange}>
							<Option value="">全部</Option>
							<Option value="0">普通订单</Option>
							<Option value="1">商户折扣订单</Option>
							<Option value="2">SAAS订单</Option>
						</Select>)}
					</FormItem>
					<FormItem>
						<Button type="primary" htmlType="submit" >查询</Button>
					</FormItem>
					<FormItem>
						<a href={`${axios.defaults.baseURL}/api/billSplit/settlementOrderList/export?${exportQs}`}>
							<Button type="primary">导出报表</Button>
						</a>

					</FormItem>
				</Form>
				<Grid url="api/settlement/billSplit/settlementOrderList" columns={this.columns} refresh={this.state.refresh} params={this.state.params}/>
			</div>
		)
	}
}

merchantLedgerOrder = createForm()(merchantLedgerOrder);
export default injectIntl(merchantLedgerOrder)